﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.FormPlugin.XHX
{
    [Description("表单插件采购价目表:计算包材单价"), HotUpdate]
    public class GetPrice : AbstractDynamicFormPlugIn
    {
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            if (e.Field.Key == "FMaterialId")
            {
                if (e.NewValue != null)
                {
                    string JiSuanGongShi = "";
                    decimal PingFangJia = 0;
                    decimal Length = 0;
                    decimal Width = 0;
                    decimal Height = 0;
                    int row = e.Row;
                    decimal price = 0;
                    string sql = string.Format(@"
                    SELECT c.F_TRPU_REMARK,c.F_TRPU_PRICE,b.FLENGTH,b.FWIDTH,b.FHEIGHT FROM dbo.T_BD_MATERIAL a
                    JOIN TRPU_t_Cust100004 c ON a.F_TRPU_BASE=c.fid
                    JOIN t_BD_MaterialBase b ON a.FMATERIALID=b.FMATERIALID
                    WHERE a.FMATERIALID='{0}'", e.NewValue);
                    DataSet ds = DBUtils.ExecuteDataSet(Context, sql);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        JiSuanGongShi = ToDBC(ds.Tables[0].Rows[0]["F_TRPU_REMARK"].ToString());
                        PingFangJia = Convert.ToDecimal(ds.Tables[0].Rows[0]["F_TRPU_PRICE"]);
                        Length = Convert.ToDecimal(ds.Tables[0].Rows[0]["FLENGTH"]);
                        Width = Convert.ToDecimal(ds.Tables[0].Rows[0]["FWIDTH"]);
                        Height = Convert.ToDecimal(ds.Tables[0].Rows[0]["FHEIGHT"]);
                        string gongShi = "";

                        gongShi = JiSuanGongShi.Replace("长", Length.ToString());
                        gongShi = gongShi.Replace("宽", Width.ToString());
                        gongShi = gongShi.Replace("高", Height.ToString());
                        gongShi = gongShi.Replace("平方价", PingFangJia.ToString());

                        sql = string.Format(@"SELECT  {0} as price", gongShi);
                        ds = DBUtils.ExecuteDataSet(Context, sql);
                        price = Convert.ToDecimal(ds.Tables[0].Rows[0]["price"]);
                        this.Model.SetValue("FTaxPrice", price, row);

                    }

                }
            }
        }
        /// <summary>
        /// 全角转半角
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private string ToDBC(string input)
        {
            char[] array = input.ToCharArray();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == 12288)
                {
                    array[i] = (char)32;
                    continue;
                }
                if (array[i] > 65280 && array[i] < 65375)
                {
                    array[i] = (char)(array[i] - 65248);
                }
            }
            return new string(array);
        }
    }
}
